package friend;

import common.AutotestUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.io.IOException;

public class LoginPage_C extends AutotestUtils {
    static String url = "http://115.190.86.135/c-oj/login";
    public LoginPage_C() {
        super(url);
    }
    // 正常登录
    // 检查登录功能 -- 成功登录
    public void loginSuccess() throws InterruptedException {
        // 先清空输入框
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入正确用户名+密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("18078555441");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("12345678");
        // 登录
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(1000);
        // 判断当前是否是成功登录的主页
        assert  driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[1]/div/div/div[1]/ul/li[1]")).getText().equals("题库");

    }






    // 检查登录功能 -- 异常登录
    public void loginFail() throws InterruptedException, IOException {

        // 先清空输入框
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入错误用户名+密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("28078555441");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("12345678");
        // 登录
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(1000);
        // 判断当前是否是成功登录的主页

        assert  driver.findElement(By.cssSelector("#app > div.oj-main-layout > div.oj-main-layout-header > div > div > div.oj-navbar-menus > ul > li:nth-child(1)")).getText().equals("题库");
        // 退出


        driver.quit();
    }
    // 检查登录功能 -- 异常登录 密码错误
    public void loginFail2() throws InterruptedException, IOException {

        // 先清空输入框
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).clear();
        // 输入正确用户名+错误密码+
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div/div/input")).sendKeys("18078555441");
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[2]/div/div/input")).sendKeys("1234561");
        // 登录
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#app > div.login-page > div > div.form-box > div.submit-box")).click();

        Thread.sleep(3000);
        // 判断当前是否是成功登录的主页

        assert  driver.findElement(By.cssSelector("#app > section > main > div.left > aside > ul > li:nth-child(1) > span")).getText().equals("用户管理");
        // 退出

        driver.quit();
    }

    // 答题
    public void answerQuestion() throws InterruptedException {
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/page-container/div[1]/div[1]/div[3]/div/div[1]/div/table/tbody/tr[1]/td[4]/div/button")).click();
        Thread.sleep(1000);
        String text = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/div[1]/div[1]/span")).getText();
        assert "题目描述".equals(text) ;

        Actions actions = new Actions(driver);
        WebElement webElement  =  driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[2]/div[1]/div/div[2]/div[2]/div"));

        actions.moveToElement(webElement)
                .click()
                .keyDown(Keys.CONTROL)
                .sendKeys("a")  // Ctrl + A 全选
                .keyUp(Keys.CONTROL)
                .sendKeys(Keys.BACK_SPACE)  // 删除选中内容
                .sendKeys("public class Solution{\n" +  // 输入新内容
                        "    public static int countCharacterOccurrences(String str, char ch){\n" +
                        "        int count = 0;\n" +
                        "         char[] arr = str.toCharArray() ;\n" +
                        "           for (char c : arr) {\n" +
                        "            if (c == ch) {\n" +
                        "                count++;\n" +
                        "            }\n" +
                        "        }\n" +
                        "      \n" +
                        "        return count;\n" +
                        "    }\n" +
                        "}")
                .perform();
        Thread.sleep(1000);

        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[1]/div[2]/div/button")).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/span")).click();

    }

    // 竞赛
    public void exam(){
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[1]/div/div/div[1]/ul/li[2]")).click();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div[2]/div/div[2]/div[1]/div[1]/div[2]/div[2]/button")).click();
        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/span")).click();

    }


}
